package softtech.apps.autoanswerphonecall;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

public class PhoneCallReceiver extends BroadcastReceiver {
	
	/**
	 * check
	 */
	static int isIncommingCall ;
	
	static String phoneNumber = "";
	
	
	MyPhoneStateListener PhoneListener;
	TelephonyManager tmgr;
	
	public PhoneCallReceiver() {
		super();
		isIncommingCall = 1;
		PhoneListener = null;
		tmgr = null;
	}
	
	@Override
	public void onReceive(Context context, Intent intent) {

		Log.e("PhoneCallReceiver+++","+++ "+intent.getAction());
		try{
			Log.e("PhoneCallReceiver+++","+++" + intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER));
			if(intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER)== null || intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER).equals(null)){
				isIncommingCall = 0;
			}else{
				
				phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
				isIncommingCall = 1;
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}

		
		try {
			AppCacheData acd = new AppCacheData();
			String enable = acd.LoadCache(context, "broadcast");
			// Log.e("PHONE","FUCK = "+enable);
			if (enable.trim().equals("0")) {
				Log.e("PHONE_EQUA", "FUCK = " + enable);
				return;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		try {

				tmgr = (TelephonyManager) context
						.getSystemService(Context.TELEPHONY_SERVICE);
				
				PhoneListener = new MyPhoneStateListener(
						context);
				
				tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

		} catch (Exception e) {
			Log.e("Phone Receive Error", " " + e);
		}
	}
}